#!/usr/bin/env python 
# -*- coding:utf-8 -*-

import pandas as pd
import numpy as np

# ps = pd.Series(np.arange(5), index=['a', 'b', 'c', 'd', 'e'])
# print(ps)
# print(ps[1:3])
# print(ps['b': 'd'])


#创建表格
#根据数组创建
# data = {
#     "id": ['a', 'b', 'c', 'd'],
#     "name": ['张三', '李四', '王大鹅', '田小鸡'],
#     "sex": ['男', '男', '女', '女']
# }
#
# df = pd.DataFrame(data)
# print(df[['name', 'id']])

#根据字典创建
# data2 = {
#     'id': {1: 'a', 2: 'b', 3: 'c'},
#     'name': {1: '张三', 2: '李四', 3: '王大鹅', 4: '田小鸡'}
# }
# df2 = pd.DataFrame(data2)
# print(df2)
# print(data2['id'])


#重建索引
# df3 = pd.DataFrame(
#     np.arange(12).reshape(4, 3),
#     index=['a', 'b', 'c', 'd'],
#     columns=['vv', 'gg', 'xx']
# )
# print(df3)
# new_df3 = df3.reindex(['b', 'a', 'd', 'c', 'e'])
# print(new_df3)
# new2_df3 = new_df3.reindex(columns=['xx', 'vv', 'gg'])
# print(new2_df3)
# print(new2_df3.drop(['a', 'e']))
# print(new2_df3.drop(['vv'], axis=1))


#创建表格
# df4 = pd.DataFrame(abs(np.random.randn(4, 3)),index=list('abcd'),columns=['z', 'x', 'c'])
# print(df4)
# fun = lambda x: x.max()-x.min()
# #默认对列进行计算
# print(df4.apply(fun,axis=0))
# #axis: 轴=1时，对行进行计算
# print(df4.apply(fun, axis=1))

#表格函数apply
# df5 = pd.DataFrame(np.random.randn(4, 3), index=list('abcd'), columns=['z', 'x', 'c'])
# print(df5)
# def fun(x):
#     return pd.Series([x.min(), x.max()], index=['min', 'max'])
# print(df5.apply(fun, axis=1))

#表格排序

df6 = pd.DataFrame(np.arange(12).reshape(4,3), index=list('acdb'),columns=['z', 'x', 'c'])
#指定轴排序
print(df6.sort_index(axis=1))
print(df6.sort_values(by='c',ascending=False))
